package com.huike.web.controller.review;


import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.exception.CustomException;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**
     * ====================================新增数据============================================
     */
    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult saveData(@PathVariable String name, @PathVariable Integer age, @PathVariable String sex) {

        try {
            Review review = new Review();
            review.setName(name);
            review.setAge(age);
            review.setSex(sex);
            System.err.println(reviewService.insertReview(review));
            return reviewService.insertReview(review);
        } catch (Exception e) {
            throw new CustomException("新增失敗");
        }
    }

    @PostMapping("/saveData")
    public AjaxResult save(@RequestBody Review review) {
        try {
            return reviewService.insertReview(review);
        } catch (Exception e) {
            throw new CustomException("新增失敗");
        }
    }

    /**删除数据*/
    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable Integer id) {
        try {
            return reviewService.deleteReview(id);
        } catch (Exception e) {
            throw new CustomException("刪除失敗 ");
        }
    }
    /**修改数据*/
    @PostMapping("/update")
    public AjaxResult updateReview(@RequestBody Review review) {
        try {
            return reviewService.updateReview(review);
        } catch (Exception e) {
            throw new CustomException("修改失敗");
        }
    }

    /**查询数据*/
    @GetMapping("/getById")
    public AjaxResult get(Integer id){
        try {
            return reviewService.selectById(id);
        } catch (Exception e) {
            throw new CustomException("查詢失敗");
        }
    }

    @GetMapping("/getDataByPage")
    public AjaxResult getDataByPage(@RequestParam Integer page,@RequestParam Integer pageSize){
        try {
            return reviewService.select(page, pageSize);
        } catch (Exception e) {
            throw new CustomException("查询失败");
        }
    }
}